home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 75 / Mac Magazin CD 75.iso / macware / Red Bird's Quick Scripts / Simple Sync / Simple Sync.as < prev   
Encoding:
Text File  |  2000-05-07  |  4.6 KB  |  99 lines  |  [TEXT/ToyS]

  1. (*
  2. Simple Sync 0.3.1:  Take any number of folders and make them match up, by way of creating a new folder with the most up to date files in it.
  3. Copyright (C) 1999 Gordon Worley.
  4.  
  5. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.
  6.  
  7. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  8.  
  9. For a copy of the GNU General Public License, visit <http://www.gnu.org/> or write to the Free Software Foundation, Inc., 59 Temple Place--Suite 330, Boston, MA 02111-1307, USA.
  10.  
  11. To contact me, please visit my Web site at <http://www.rbisland.cx/> or e-mail me at <redbird@rbisland.cx>.
  12.  
  13. History:
  14.  
  15. 0.3.1 - Fixed the bug and bumped the number to reflect the fact that a few changes were made during the betas.
  16. 0.3b1 - Didn't fix the bug, thus the beta status.  Did make the open handler work, though.  Also changed 'me's' to 'my' to further promote English-like code.
  17. 0.3b0 - Much of the code has been changed around.  Every thing seems to work now, but sometimes I get an error in sync() that I've noted below.
  18. 0.1.5 - Commented the code so that others will be able to make some sense out of it.
  19. 0.1 - Added support for resyncing the original folders used in making syncedFolder and deleting syncedFolder when done with it.
  20. 0.0 - Initial release.
  21. *)
  22.  
  23. on run
  24.     try
  25.         set folder1 to choose folder with prompt "Choose first folder to sync:"
  26.     on error --user probably pressed cancle and wants to exit the program
  27.         return
  28.     end try
  29.     try
  30.         set folder2 to choose folder with prompt "Choose second folder to sync:"
  31.     on error
  32.         return
  33.     end try
  34.     my autoSync({folder1, folder2})
  35. end run
  36.  
  37. on open theFolders
  38.     repeat with aFolder in theFolders
  39.         if alias (aFolder as string)'s kind is not "folder" then
  40.             display dialog "Sorry, but at least one of the objects you dropped on this application were not folders.  Please try again with only folders." buttons "OK" default button 1
  41.             return
  42.         end if
  43.         my autoSync(theFolders)
  44.     end repeat
  45. end open
  46.  
  47. on autoSync(theFolders)
  48.     try
  49.         tell application "Finder" to set syncedFolder to make folder with properties {name:"Synced Folder"}
  50.     on error
  51.         --this is usually because, as the dialog says, some folder named 'Synced Folder' already exist
  52.         --sometimes, though, it is because you have made a stupid mistake, like forgetting to *tell the Finder* to do the making of new folders
  53.         display dialog "Sorry, but a folder \"Synced Folder\" already exist.  Move \"Synced Folder\" off the Desktop and then try again." buttons "OK" default button 1
  54.         return
  55.     end try
  56.     my sync(theFolders, syncedFolder)
  57.     my resync(theFolders, syncedFolder)
  58.     my cleanUp(syncedFolder)
  59. end autoSync
  60.  
  61. on sync(theFolders, syncedFolder)
  62.     tell application "Finder"
  63.         repeat with aFolder in theFolders
  64.             --these next two lines make a list of everything (folder, file, application) in the respective folders
  65.             --note how below an alias must be built using the path to the folder that was gotten in the arguments and combining it with an item to form a full alias
  66.             set syncedFolderItems to list folder syncedFolder
  67.             set aFolderItems to list folder aFolder
  68.             repeat with anItem in aFolderItems
  69.                 if anItem is not in syncedFolderItems then
  70.                     duplicate alias ((aFolder as string) & anItem) to syncedFolder
  71.                 else --anItem is not in syncedFolderItems
  72.                     if alias ((aFolder as string) & anItem)'s kind is "folder" then
  73.                         --note how ':' must be added after anItem so that it is a folder rather than a file when as an alias
  74.                         my sync({(alias ((aFolder as string) & anItem & ":"))}, (alias ((syncedFolder as string) & anItem & ":")))
  75.                     else if alias ((aFolder as string) & anItem)'s modification date > alias ((syncedFolder as text) & anItem)'s modification date then --use the newest one
  76.                         duplicate alias ((aFolder as string) & anItem) to syncedFolder with replacing
  77.                     end if
  78.                 end if
  79.             end repeat
  80.         end repeat
  81.     end tell
  82. end sync
  83.  
  84. on resync(theFolders, syncedFolder)
  85.     tell application "Finder"
  86.         repeat with aFolder in theFolders
  87.             --thanks to John Louch for helping with this part on the applescript-users mailing list
  88.             set aFolderName to aFolder's name
  89.             set syncedFolderToMove to duplicate syncedFolder
  90.             set parentFolder to syncedFolderToMove's container
  91.             set syncedFolderToMove's name to aFolderName
  92.             move parentFolder's folder aFolderName to aFolder's container with replacing
  93.         end repeat
  94.     end tell
  95. end resync
  96.  
  97. on cleanUp(syncedFolder)
  98.     delete syncedFolder
  99. end cleanUp